<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article#
" lang="zh_cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>推荐fody库 | vitamind3 竹里馆</title>
<link href="../../assets/css/all-nocdn.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/ipython.min.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/nikola_ipython.css" rel="stylesheet" type="text/css">
<meta name="theme-color" content="#5670d4">
<meta name="generator" content="Nikola (getnikola.com)">
<link rel="alternate" type="application/rss+xml" title="RSS" hreflang="zh_cn" href="../../rss.xml">
<link rel="canonical" href="https://vitamind3.gitee.io/posts/tui-jian-fodyku/">
<!--[if lt IE 9]><script src="../../assets/js/html5.js"></script><![endif]--><meta name="author" content="vitamind3">
<link rel="prev" href="../fa-bu-obsidiancang-ku-mo-ban-02ban/" title="发布obsidian仓库模板0.2版" type="text/html">
<link rel="next" href="../zi-dong-sheng-cheng-lei-shi-xian-jie-kou/" title="自动生成类实现接口" type="text/html">
<meta property="og:site_name" content="vitamind3 竹里馆">
<meta property="og:title" content="推荐fody库">
<meta property="og:url" content="https://vitamind3.gitee.io/posts/tui-jian-fodyku/">
<meta property="og:description" content="最近两年在C#项目里广泛的使用了fody库，感觉挺不错，这里做个推荐。简单说Fody的功能是在编译阶段修改生成的IL代码，添加一些功能。

主页：GitHub - Fody/Fody: Extensible tool for weaving .net assemblies 。
能添加哪些功能呢？看这里 Home/addins.md at master · Fody/Home · GitHub 。
">
<meta property="og:type" content="article">
<meta property="article:published_time" content="2022-03-26T22:31:46+08:00">
<meta property="article:tag" content=".net">
<meta property="article:tag" content="C#">
<meta property="article:tag" content="nuget">
</head>
<body>
<a href="#content" class="sr-only sr-only-focusable">跳到主内容</a>

<!-- Menubar -->

<nav class="navbar navbar-expand-md static-top mb-4
navbar-dark
bg-dark
"><div class="container">
<!-- This keeps the margins nice -->
        <a class="navbar-brand" href="../../">

            <span id="blog-title">vitamind3 竹里馆</span>
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#bs-navbar" aria-controls="bs-navbar" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="bs-navbar">
            <ul class="navbar-nav mr-auto">
<li class="nav-item">
<a href="../../pages/introduction" class="nav-link">欢迎</a>
                </li>
<li class="nav-item">
<a href="../../archive.html" class="nav-link">文章归档</a>
                </li>
<li class="nav-item">
<a href="../../categories/" class="nav-link">分类</a>
                </li>
<li class="nav-item">
<a href="../../rss.xml" class="nav-link">RSS 源</a>

                
            </li>
</ul>
<ul class="navbar-nav navbar-right"></ul>
</div>
<!-- /.navbar-collapse -->
    </div>
<!-- /.container -->
</nav><!-- End of Menubar --><div class="container" id="content" role="main">
    <div class="body-content">
        <!--Body content-->
        
        
        
<article class="post-text h-entry hentry postpage" itemscope="itemscope" itemtype="http://schema.org/Article"><header><h1 class="p-name entry-title" itemprop="headline name"><a href="." class="u-url">推荐fody库</a></h1>

        <div class="metadata">
            <p class="byline author vcard p-author h-card"><span class="byline-name fn p-name" itemprop="author">
                    vitamind3
            </span></p>
            <p class="dateline">
            <a href="." rel="bookmark">
            <time class="published dt-published" datetime="2022-03-26T22:31:46+08:00" itemprop="datePublished" title="2022-03-26 22:31">2022-03-26 22:31</time></a>
            </p>
            

        </div>
        

    </header><div class="e-content entry-content" itemprop="articleBody text">
    <div>
<p>最近两年在C#项目里广泛的使用了fody库，感觉挺不错，这里做个推荐。简单说Fody的功能是在<strong>编译</strong>阶段修改生成的IL代码，添加一些功能。</p>
<ul>
<li>主页：<a href="https://github.com/Fody/Fody">GitHub - Fody/Fody: Extensible tool for weaving .net assemblies</a> 。</li>
<li>能添加哪些功能呢？看这里 <a href="https://github.com/Fody/Home/blob/master/pages/addins.md">Home/addins.md at master · Fody/Home · GitHub</a> 。</li>
</ul>
<p>其中一些我经常用到的扩展说明一下：</p>
<!-- TEASER_END -->

<ul>
<li>Anotar , 适配了多个logging库，极大简化logging调用。</li>
<li>Equals , 生成 Equals, GetHashCode 重载。一个类似的扩展是 Equatable ，我更喜欢用这个。</li>
<li>Janitor , 简化实现 IDispose 。</li>
<li>MethodTimer , 计时。</li>
<li>RuntimeNullables , 对 C# 8.0 的 NRT 类型做判断，即检查非空类型，如果为空即抛异常。这个扩展我现在必用。</li>
<li>ToString, 实现 ToString() ，有价值。</li>
<li>MethodDecorator, 可为方法添加执行前、执行后的代码。</li>
<li>MethodCache, 类似于 Python 中的 lru ，缓存方法的结果。</li>
</ul>
<p>除了 Fody ，还有没有类似的工具？当然有，用 source generator 也能自动生成代码，但与Fody还是有差别，例如source generator 无法实现RuntimeNullables这样的功能。如果自己想开发类似功能，现在优先的选项是 source generator 。 如果不想自己动手，那可以直接使用 Fody 项目中这些已经积累的 addins ，质量都不错。</p>
</div>
    </div>
    <aside class="postpromonav"><nav><ul itemprop="keywords" class="tags">
<li><a class="tag p-category" href="../../categories/net/" rel="tag">.net</a></li>
            <li><a class="tag p-category" href="../../categories/c/" rel="tag">C#</a></li>
            <li><a class="tag p-category" href="../../categories/nuget/" rel="tag">nuget</a></li>
        </ul>
<ul class="pager hidden-print">
<li class="previous">
                <a href="../fa-bu-obsidiancang-ku-mo-ban-02ban/" rel="prev" title="发布obsidian仓库模板0.2版">上一篇文章</a>
            </li>
            <li class="next">
                <a href="../zi-dong-sheng-cheng-lei-shi-xian-jie-kou/" rel="next" title="自动生成类实现接口">下一篇文章</a>
            </li>
        </ul></nav></aside></article><!--End of body content--><footer id="footer">
            Contents ©         <a href="https://vitamind3.gitee.io/">vitamind3</a> - Powered by         <a href="https://getnikola.com" rel="nofollow">Nikola</a>         
            
            
        </footer>
</div>
</div>


        <script src="../../assets/js/all-nocdn.js"></script><script>
    baguetteBox.run('div#content', {
        ignoreClass: 'islink',
        captions: function(element){var i=element.getElementsByTagName('img')[0];return i===undefined?'':i.alt;}});
    </script><script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?09027755f7478e129aec2cdd3acf9a4c";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>
</body>
</html>
